//
//  GTGuideWebViewBottomToolBar.m
//  GiftTalk
//
//  Created by Wan on 16/4/6.
//  Copyright © 2016年 Wan. All rights reserved.
//

#import "GTGuideWebViewBottomToolBar.h"
#import "GTVerticalButton.h"
#import "GTGuideDetail.h"
#import "GTUMSocialShare.h"
#import "GTGuideDetailViewController.h"
#import "GTCommentViewController.h"

@interface GTGuideWebViewBottomToolBar ()

@property (weak, nonatomic) IBOutlet GTVerticalButton *favoriteButton;
@property (weak, nonatomic) IBOutlet GTVerticalButton *shareButton;
@property (weak, nonatomic) IBOutlet GTVerticalButton *replyButton;

- (IBAction)favoriteButtonClick:(GTVerticalButton *)sender;
- (IBAction)shareButtonClick:(GTVerticalButton *)sender;
- (IBAction)replyButtonClick:(GTVerticalButton *)sender;

@end

@implementation GTGuideWebViewBottomToolBar

+ (instancetype)guideWebViewBottomToolBar {
    return [self viewFromXib];
}

- (void)setGuideDetail:(GTGuideDetail *)guideDetail {
    _guideDetail = guideDetail;
    
    if (self.guideDetail.liked) {
        self.favoriteButton.selected = self.guideDetail.liked;
    }
    
    if (self.guideDetail.likes_count > 10000) {
        CGFloat likeCount = self.guideDetail.likes_count / 10000.0;
        self.favoriteButton.titleLabel.text = [NSString stringWithFormat:@"%.1f万人喜欢",likeCount];
    } else {
        self.favoriteButton.titleLabel.text = [NSString stringWithFormat:@"%ld人喜欢",self.guideDetail.likes_count];
    }
    
    self.shareButton.titleLabel.text = [NSString stringWithFormat:@"%zd次分享",self.guideDetail.shares_count];
    self.replyButton.titleLabel.text = [NSString stringWithFormat:@"%zd条评论",self.guideDetail.comments_count];
}

- (IBAction)favoriteButtonClick:(GTVerticalButton *)sender {
    
    NSInteger likeCount = sender.selected ? self.guideDetail.likes_count + 1 : self.guideDetail.likes_count - 1;
    GTLog(@"likeCount: %ld, +1 : %ld",likeCount, likeCount + 1);
    
    if (likeCount > 10000) {
        CGFloat count = likeCount / 10000.0;
        self.favoriteButton.titleLabel.text = [NSString stringWithFormat:@"%.1f万人喜欢",count];
    } else {
        self.favoriteButton.titleLabel.text = [NSString stringWithFormat:@"%ld人喜欢", sender.selected ? likeCount + 1 : likeCount - 1];
    }
    
    sender.selected = !sender.selected;
}

- (IBAction)shareButtonClick:(GTVerticalButton *)sender {
    UIViewController *rootVc = [UIApplication sharedApplication].keyWindow.rootViewController;
    [GTUMSocialShare shareByPresentVc:rootVc shareImage:[UIImage imageNamed:@"post_sharebutton_highlighted"] shareDescription:nil shareUrl:nil];
}

- (IBAction)replyButtonClick:(GTVerticalButton *)sender {
    GTLogFunc
    
    /*
    // 根据根控制器,找到跟控制器 modal 出的控制器, 所对应的导航控制器
    UIViewController *rootVc = [UIApplication sharedApplication].keyWindow.rootViewController;
    UINavigationController *nav = (UINavigationController *)rootVc.presentedViewController;
    
    GTCommentViewController *commentVc = [[GTCommentViewController alloc] init];
    [nav pushViewController:commentVc animated:YES];
    */

}

- (void)addReplyButtonTarget:(id)target action:(SEL)action {
    [self.replyButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}

/*
 - (void)addFavoriteButtonTarget:(id)target action:(SEL)action {
 [self.favoriteButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
 }
 
 - (void)addShareButtonTarget:(id)target action:(SEL)action {
 [self.shareButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
 }
 
 */
@end
